//
//  LeftDrawer.m
//  niminban
//
//  Created by zhouteng on 16/9/30.
//  Copyright © 2016年 zhouteng. All rights reserved.
//

#import "LeftDrawer.h"
#import "UIViewController+MMDrawerController.h"
#import "AFNetworking.h"
#import "URLS.h"
#import "ForumGroup.h"
#import "MJExtension.h"
#import "Forum.h"
#import "MainViewController.h"
#import "Masonry.h"

@implementation LeftDrawer

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self getForumList];
}

-(void)initTableView{
    _tableView = [[UITableView alloc]init];
    
 
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    NSMutableArray *arrForTable = [[NSMutableArray alloc]init];
    NSInteger count = -1;
    
    for (ForumGroup *fg in _ForumList) {
        count++;
         NSInteger count2 = -1;
        [arrForTable addObject:
         [NSArray arrayWithObjects:[NSNumber numberWithInteger:count],[NSNumber numberWithInteger:count2], nil]];
    
        for (Forum *f in fg.forums) {
            count2++;
            [arrForTable addObject:
             [NSArray arrayWithObjects:[NSNumber numberWithInteger:count],[NSNumber numberWithInteger:count2], nil]];
        }
    }
    
    _arrForTable = arrForTable;
    
    NSLog(@"%@",arrForTable);
    
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *id = [self getId:indexPath.row];
    if(id){
        MainViewController *controller = (MainViewController *)self.mm_drawerController.centerViewController;
        [controller update:id page:1];
        [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifierLevle0 = @"Levle0";
    static NSString *CellIdentifierLevle1 = @"Levle1";
    
    UITableViewCell *cell;
    //是否为组
    if([self isGroup:indexPath.row]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLevle0];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLevle0];
        }
        
        cell.textLabel.text = [self getName:indexPath.row];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLevle1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLevle1];
        }
        cell.textLabel.text = [self getName:indexPath.row];
        
        [cell setIndentationLevel:2];
    }
    
    return cell;
}

-(BOOL)isGroup:(NSInteger)index{
    NSArray *position = [_arrForTable objectAtIndex:index];
    return [[position objectAtIndex:1]isEqualToNumber:@-1];
}

-(NSString *)getName:(NSInteger)index{
    NSArray *position = [_arrForTable objectAtIndex:index];
    if([self isGroup:index]){
        return [_ForumList objectAtIndex:
                [[position objectAtIndex:0] integerValue]].name;
    }else{
        return [[[_ForumList objectAtIndex:
                  [[position objectAtIndex:0] integerValue]]
                 forums] objectAtIndex:[[position objectAtIndex:1] integerValue]].name;
    }
}

-(NSString *)getId:(NSInteger )index{
    NSArray *position = [_arrForTable objectAtIndex:index];
    if([self isGroup:index]){
        return nil;
    }else{
        return [[[_ForumList objectAtIndex:
                  [[position objectAtIndex:0] integerValue]]
                 forums] objectAtIndex:[[position objectAtIndex:1] integerValue]].id;
    }
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrForTable.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


//    ForumGroup *forumGroup=[_ForumList objectAtIndex:indexPath.row];
//    NSArray<Forum *> *forums =forumGroup.forums;
//    if(forums.count > 0) {
//        
//                    NSUInteger count=indexPath.row+1;
//                    NSMutableArray *arCells=[NSMutableArray array];
//                    for(Forum *forum in forums ) {
//                        [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
//                    }
//                    [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
    
//        BOOL isAlreadyInserted=NO;
//        for(Forum *forum in forums){
//            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
//            isAlreadyInserted=(index>0 && index!=NSIntegerMax);
//            if(isAlreadyInserted) break;
//        }
//        if(isAlreadyInserted) {
//            [self miniMizeThisRows:ar];
//        } else {
//            NSUInteger count=indexPath.row+1;
//            NSMutableArray *arCells=[NSMutableArray array];
//            for(NSDictionary *dInner in ar ) {
//                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
//                [self.arForTable insertObject:dInner atIndex:count++];
//            }
//            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
//        }
//    }
//}

//-(void)miniMizeThisRows:(NSArray*)ar{
//    for(NSDictionary *dInner in ar ) {
//        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
//        NSArray *arInner=[dInner valueForKey:@"Objects"];
//        if(arInner && [arInner count]>0){
//            [self miniMizeThisRows:arInner];
//        }
//        if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
//            [self.arForTable removeObjectIdenticalTo:dInner];
//            [self.tableView deleteRowsAtIndexPaths:
//             [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]]
//                                  withRowAnimation:UITableViewRowAnimationRight];
//        }
//    }
//}

-(void)getForumList{
    NSString *URLString = GET_FORUM_LIST;
    NSLog(GET_FORUM_LIST);
    AFHTTPSessionManager *magager = [AFHTTPSessionManager manager];
    
    magager.responseSerializer = [AFJSONResponseSerializer serializer  ];
    
    [magager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        _ForumList = [ForumGroup mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self initTableView];
        
        NSLog(@"%@",_ForumList);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error");
    }];
}

@end
